package com.ruoyi.project.module.lys;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.project.domain.YqgConsumptionDetails;
import com.ruoyi.project.service.IYqgConsumptionDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.YqgBarBills;
import com.ruoyi.project.service.IYqgBarBillsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 吧台账单Controller
 *
 * @author dr
 * @date 2023-04-17
 */
@RestController
@RequestMapping("/pbills")
public class LysYqgBarBillsController extends BaseController {
    @Autowired
    private IYqgBarBillsService yqgBarBillsService;
    @Autowired
    private IYqgConsumptionDetailsService yqgConsumptionDetailsService;

    /**
     * 查询吧台账单列表
     */
//    @RequiresPermissions("project:bills:list")
    @GetMapping("/list")
    public TableDataInfo list(YqgBarBills yqgBarBills) {
        startPage();
        List<YqgBarBills> list = yqgBarBillsService.selectYqgBarBillsList(yqgBarBills);
        return getDataTable(list);
    }

    /**
     * 导出吧台账单列表
     */
//    @RequiresPermissions("project:bills:export")
    @Log(title = "吧台账单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqgBarBills yqgBarBills) {
        List<YqgBarBills> list = yqgBarBillsService.selectYqgBarBillsList(yqgBarBills);
        ExcelUtil<YqgBarBills> util = new ExcelUtil<YqgBarBills>(YqgBarBills.class);
        util.exportExcel(response, list, "吧台账单数据");
    }

    /**
     * 获取吧台账单详细信息
     */
//    @RequiresPermissions("project:bills:query")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID) {
        return success(yqgBarBillsService.selectYqgBarBillsByID(ID));
    }

    /**
     * 新增吧台账单
     */
//    @RequiresPermissions("project:bills:add")
    @Log(title = "吧台账单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YqgBarBills yqgBarBills) {
        return toAjax(yqgBarBillsService.insertYqgBarBills(yqgBarBills));
    }

    /**
     * 修改吧台账单
     */
//    @RequiresPermissions("project:bills:edit")
    @Log(title = "吧台账单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqgBarBills yqgBarBills) {
        return toAjax(yqgBarBillsService.updateYqgBarBills(yqgBarBills));
    }

    /**
     * 删除吧台账单
     */
//    @RequiresPermissions("project:bills:remove")
    @Log(title = "吧台账单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs) {
        return toAjax(yqgBarBillsService.deleteYqgBarBillsByIDs(IDs));
    }

    /**
     * 修改交班功能
     */
//    @RequiresPermissions("project:bills:alter")
    @Log(title = "交班功能", businessType = BusinessType.UPDATE)
    @PutMapping("/pp")
    public AjaxResult alter(@RequestBody YqgBarBills yqgBarBills) {





        YqgBarBills yqgBarBills2=new YqgBarBills();
        yqgBarBills2.setHandoverStatus("0");
        List<YqgBarBills> list1 = yqgBarBillsService.selectYqgBarBillsList(yqgBarBills2);
        for(YqgBarBills p:list1){

            //未交班，已结账
            if (p.getHandoverStatus().equals("0")) {

                YqgConsumptionDetails s=new YqgConsumptionDetails();
                s.setBarBillsId(p.getID());

                List<YqgConsumptionDetails> list2= yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(s);
               for (YqgConsumptionDetails yqgConsumptionDetails1: list2) {
                   if (yqgConsumptionDetails1.getHandoverStatus().equals("0") && yqgConsumptionDetails1.getHandState().equals("1")) {

                       yqgConsumptionDetails1.setHandoverStatus("1");
                       yqgConsumptionDetails1.setSailingsSigns("1");
                       yqgConsumptionDetailsService.updateYqgConsumptionDetails(yqgConsumptionDetails1);


                   }
               }
                System.out.println(p.getHandState()+"88");
                p.setHandoverStatus("1");
                p.setSailings(yqgBarBills.getSailings());
                Date d = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String dd = sdf.format(d);
                p.setHandoverTime(dd);
                //我天哪
                yqgBarBillsService.updateYqgBarBills(p);
            }

        }
//u
       /* yqgBarBills.setSailings("1");
        return toAjax(yqgBarBillsService.updateYqgBarBillsHandoverTime(yqgBarBills));*/
       return  success();
    }
    /**
     * 修改备注功能
     */
//    @RequiresPermissions("project:bills:revise")
    @Log(title = "修改备注", businessType = BusinessType.UPDATE,logicalKey = "handBrand")
    @PutMapping("/pp1")
    public AjaxResult revise(@RequestBody YqgBarBills yqgBarBills) {


        if(!yqgBarBills.getTogether().isEmpty()) {
            String[] dataArray =yqgBarBills.getTogether().split(",");




//                String s = yqgBarBills.getTogether();//获取同来手牌号
                YqgBarBills yqgBarBills1 = new YqgBarBills();//新建对象 是空的
            for (String i:dataArray) {
                System.out.println(i+"11111");
                yqgBarBills1.setHandState("1");
                yqgBarBills1.setHandBrand(i);//设置需要的状态
                List<YqgBarBills> list = yqgBarBillsService.selectYqgBarBillsList(yqgBarBills1);//查同来的手牌数据
                for (YqgBarBills p : list) {
                    p.setGroupNotes(yqgBarBills.getGroupNotes());
                    yqgBarBillsService.updateYqgBarBills(p);
                }
            }
        }



        return toAjax(yqgBarBillsService.updateYqgBarBills(yqgBarBills));



       /* 多条用list，单条直接修改
       yqgBarBills.setSailings("1");
        return toAjax(yqgBarBillsService.updateYqgBarBillsHandoverTime(yqgBarBills));*/
        // return  success();
    }







}